import requests
from bs4 import BeautifulSoup
import csv

url = 'https://car.autohome.com.cn'
response = requests.get(url)
content = response.content

soup = BeautifulSoup(content, 'html.parser')

brands = soup.select('.tab-content-item.current .brand-item')

data = []
for brand in brands:
    brand_name = brand.select_one('.font-brand').text.strip()
    submodels = brand.select('.interval01-list .interval01-list-cars')
    for submodel in submodels:
        submodel_name = submodel.select_one('.interval01-list-model').text.strip()
        data.append([brand_name, submodel_name])

with open('car_data.csv', 'w', newline='', encoding='utf-8') as f:
    writer = csv.writer(f)
    writer.writerow(['品牌', '子型号'])
    writer.writerows(data)
